/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;

namespace Xceed.Silverlight.Data
{
  internal class SubCollection<T> : IDataEnumerable<T>
  {
    public SubCollection( IDataEnumerable<T> allItems )
    {
      m_allItems = allItems;
    }

    public event EventHandler<SubCollectionChangedEventArgs> SubCollectionChanged;
    internal event EventHandler<SubCollectionChangedEventArgs> SubCollectionChanging;

    internal bool ContainsItems
    {
      get
      {
        return ( m_reverse ) || ( m_containedItems.Count > 0 );
      }
    }

    protected internal bool IsReversed
    {
      get
      {
        return m_reverse;
      }
    }

    protected internal IList<T> InclusionList
    {
      get
      {
        return m_containedItems;
      }
    }

    public void IncludeAll()
    {
      Debug.Assert( m_allItems != null, "GetEnumerator() will throw on this reversed list. Only Contains can be called." );
      this.RaiseSubCollectionChanging( SubCollectionChangedAction.IncludeAll, null );
      m_reverse = true;
      m_containedItems.Clear();
      this.RaiseSubCollectionChanged( SubCollectionChangedAction.IncludeAll, null );
    }

    public void Clear()
    {
      this.RaiseSubCollectionChanging( SubCollectionChangedAction.Clear, null );
      m_reverse = false;
      m_containedItems.Clear();
      this.RaiseSubCollectionChanged( SubCollectionChangedAction.Clear, null );
    }

    public virtual void Add( T item )
    {
      Validate( item, "The specified item is invalid and cannot be added." );

      this.RaiseSubCollectionChanging( SubCollectionChangedAction.Add, item );

      if( m_reverse )
      {
        m_containedItems.Remove( item );
      }
      else if( !m_containedItems.Contains( item ) )
      {
        m_containedItems.Add( item );
      }

      this.RaiseSubCollectionChanged( SubCollectionChangedAction.Add, item );
    }

    public virtual void Remove( T item )
    {
      Validate( item, "The specified item is invalid and cannot be removed." );

      this.RaiseSubCollectionChanging( SubCollectionChangedAction.Remove, item );

      if( !m_reverse )
      {
        m_containedItems.Remove( item );
      }
      else if( !m_containedItems.Contains( item ) )
      {
        m_containedItems.Add( item );
      }

      this.RaiseSubCollectionChanged( SubCollectionChangedAction.Remove, item );
    }

    public bool Contains( T item )
    {
      return ( m_reverse ) ^ ( m_containedItems.Contains( item ) );
    }

    protected virtual void ValidateItem( T item )
    {
    }

    private void Validate( T item, string message )
    {
      try
      {
        ValidateItem( item );
      }
      catch( Exception e )
      {
        throw new ArgumentException( message, e );
      }
    }

    private void RaiseSubCollectionChanged( SubCollectionChangedAction action, object target )
    {
      if( this.SubCollectionChanged != null )
      {
        this.SubCollectionChanged( this, new SubCollectionChangedEventArgs( action, target ) );
      }
    }

    private void RaiseSubCollectionChanging( SubCollectionChangedAction action, object target )
    {
      if( this.SubCollectionChanging != null )
      {
        this.SubCollectionChanging( this, new SubCollectionChangedEventArgs( action, target ) );
      }
    }

    #region IDataEnumerable<T> Members

    public IAsyncEnumerator<T> GetAsyncDataEnumerator()
    {
      if( m_reverse && ( m_allItems == null ) )
        throw new InvalidOperationException( "At attempt was made to retrieve the enumerator for this collection when all items were not specified at creation." );

      Predicate<T> filter;
      if( m_reverse && ( m_containedItems.Count == 0 ) )
      {
        //The only time that the filtering is Disabled, is when
        //the collection is "reversed" case with no inclusion. (ex. Select All with no de-selection)
        filter = null;
      }
      else
      {
        filter = (item) => this.Contains( item );
      }

      return new FilterEnumerator<T>( m_allItems.GetAsyncDataEnumerator(), filter );
    }

    #endregion

    private IDataEnumerable<T> m_allItems;
    private List<T> m_containedItems = new List<T>();
    private bool m_reverse;//false;
  }
}
